
function initCurrentUser() {
	$$ajax({
		url: "/agent/current",
		type: "get",
		contentType: 'application/json',
		success: function (response) {
			if (response.code == 200) {
				console.log("查询成功");
				$("#accountName").text(response.data.agentName);
				menuArr(response.data.userType);
			} else if (response.code == 401) {
				window.location.href = "login.html";
			}
		},
		error: function (textStatus) {

			alert(textStatus);
			window.location.href = "login.html";
			console.error(textStatus);
		},
		complete: function (XMLHttpRequest, statusText) {
			if (statusText == 'timeout') {
				xhr.abort();
				alert('Time out');
			}
		},
	});
}
var __logout = "./agent/logout"; //post 方式 退出登录
$(function () {
	 	setTimeout(initCurrentUser, 400);
})

function menuArr (userType){
	if(userType==1){
		var menuArr = [{ menuName: "团队管理", id: 1, parentId: 0, permission: "app" }, { menuName: "我的团队", id: 2, parentId: 1, leaf: "true", permission: "app.agent" }, { menuName: "我的代理收益", id: 3, parentId: 1, leaf: "true", permission: "app.record" }, { menuName: "我推广的用户", id: 4, parentId: 1, leaf: "true", permission: "app.member" }];
	} else if(userType==2){
		var menuArr = [{ menuName: "团队管理", id: 1, parentId: 0, permission: "app" }, { menuName: "我的讲师团队", id: 2, parentId: 1, leaf: "true", permission: "app.expert" }, { menuName: "我的代理收益", id: 3, parentId: 1, leaf: "true", permission: "app.record" }];
	}
	for (var i = 0; i < menuArr.length; i++) {
		if (menuArr[i].show == "false") {
			menuArr.splice(i, 1);
			i--;
		}
	}
	var arr = [];
	for (var i = 0; i < menuArr.length; i++) {
		if (menuArr[i].parentId == 0) {
			arr.push(menuArr[i]);
			$("#side-menu").append("<li><a href='#'><i class='fa fa-home'></i><span class='nav-label' >" + menuArr[i].menuName + "</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' id='" + menuArr[i].id + "'></ul></li>")
			menuArr.splice(i, 1);
			i--;
		}
	}
	initMenu(arr, menuArr);
	$.initMetsiMenu();
}
function initMenu(fatherArray, sonArray) {
	var newArray = [];
	for (var i in fatherArray) {
		for (var j = 0; j < sonArray.length; j++) {
			if (sonArray[j].parentId == fatherArray[i].id) {
				if (sonArray[j].leaf == "true") {
				 
					var name = sonArray[j].permission.replace(/\./g, "_");
					$("#" + fatherArray[i].id).append("<li><a class='J_menuItem' href='html/" + name + "/list.html?' data-index='0'>" + sonArray[j].menuName + "</a></li>");
					sonArray.splice(j, 1);
					j--;
				} else {
					$("#" + fatherArray[i].id).append("<li><a href='#'><i class='fa fa-home'></i><span class='nav-label' >" + sonArray[j].menuName + "</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' id='" + sonArray[j].id + "'></ul></li>");
					sonArray.splice(j, 1);
					newArray.push(sonArray[j]);
					j--;
				}
			}
		}
	}
	if (newArray.length > 0) {
		initMenu(newArray, sonArray);
	}
}
$(".logout").click(function () {
	logout();
})
function logout() {
	$$ajax({
		url: __logout,
		type: 'post',//提交的方式
		dataType: 'json',
		async: true,
		contentType: "application/json; charset=utf-8",
		success: function (data) {
			if (data.code == 200) {
				console.log(data.msg)
				//alert(data.msg)
			} else {
				alert(data.msg);
			}
		}

	});
}








// var __logout = "../logout"; //post 方式 退出登录
// var __getMenu = "/user/read/current/menu";
// $(function() {

//     setTimeout(initCurrentUser(),400);
// 	function initCurrentUser(){
// 		var menuArr = [1];
// 		var arr = [0];

// 		$$ajax({
// 			url: "/agent/current",
// 			type: "get",
// 			contentType: 'application/json',
// 			timeout: 1000,    

// 			success: function(response) {

// 			   if (response.code == 200) {   
// 					$("#accountName").text(response.data.agentName);
// 					//$("#userName").html(response.data.userName+"<b class='caret'>");
// 					//$("#side-menu").append("<li>米振</li>")
// 				if(response.data.userType==1){
// 					$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的团队</a></li><li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的收益列表</a></li>");
// 					initMenu(arr,menuArr);
// 					$.initMetsiMenu();
// 					//$("#side-menu").append("<li><a href='./html/app_agent/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>")
// 				}else if(response.data.userType==2){
// 						$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_expert/list.html?id="+response.data.id+"' data-index='0'>我的讲师团队</a></li><li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的收益列表</a></li>");
// 					//$("#side-menu").append("<li><a href='./html/app_expert/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的讲师团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>");				
// 					initMenu(arr,menuArr);
// 					$.initMetsiMenu();
// 				}

// 				} else if(response.code == 401){
// 					window.location.href = "login.html";							
// 				}

// 				},
// 				error: function (textStatus) {

// 					alert(textStatus);
// 					window.location.href = "login.html";		
// 					console.error(textStatus);
// 				},
// 				complete: function (XMLHttpRequest, statusText) {
// 					if (statusText == 'timeout') {
// 						xhr.abort();  
// 						alert('Time out');
// 					}
// 				},
// 				//tableInit.myViewModel.refresh();
// 			});
// 	 }


// })

// function initMenu (fatherArray,sonArray) {
// 	var newArray = [];
// 	for(var i in fatherArray){
// 		for(var j = 0;j<sonArray.length;j++){
// 			if(sonArray[j].parentId==fatherArray[i].id){
// 				if(sonArray[j].leaf=="true"){
// 					var name = sonArray[j].permission.replace(/\./g,"_");
// 					$("#"+fatherArray[i].id).append("<li><a class='J_menuItem' href='html/"+name+"/list.html' data-index='0'>"+sonArray[j].menuName+"</a></li>");
// 					sonArray.splice(j,1);
// 					j--;
// 				}else{
// 					$("#"+fatherArray[i].id).append("<li><a href='#'><i class='fa fa-home'></i><span class='nav-label' >"+sonArray[j].menuName+"</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' id='"+sonArray[j].id+"'></ul></li>");
// 					sonArray.splice(j,1);
// 					newArray.push(sonArray[j]);
// 					j--;
// 				}
// 			}
// 		}
// 	}
// 	if(newArray.length>0){
// 		initMenu(newArray,sonArray);
// 	}
// }
$(".logout").click(function () {
	logout();
})
function logout() {
	$$ajax({
		url: __logout,
		type: 'post',//提交的方式
		dataType: 'json',
		async: true,
		contentType: "application/json; charset=utf-8",
		success: function (data) {
			if (data.httpCode == 200) {
				console.log(data.msg)
				//alert(data.msg)
			} else {
				alert(data.msg);
			}
		}

	});
}
